package handler

import (
	"github.com/name5566/leaf/gate"
	"server/game/translate"
	"server/msg/jsonData"
)

func CreateTranslateInfo(args []interface{}) {
	m := args[0].(*jsonData.MsgCreateTranslateInfoReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	translateInfoList, err := translate.CreateTranslateInfo(m.UserId, m.TId)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, RequestId: m.RequestId})
		return
	}

	var translateInfoListMsg []jsonData.TranslateInfo
	for _, translateInfoTmp := range translateInfoList {
		// 将 Task 转换为 msg.TaskInfo
		taskInfo := jsonData.TranslateInfo{
			UserId:      translateInfoTmp.UserId,
			TId:         translateInfoTmp.T_id,
			TTid:        translateInfoTmp.TT_id,
			Level:       translateInfoTmp.Level,
			ExpandLevel: translateInfoTmp.ExpandLevel,
			DaoSaoCiShu: translateInfoTmp.DaoSaoCiShu,
		}
		for _, cardTool := range translateInfoTmp.CardTooles {
			taskInfo.CardTooles = append(taskInfo.CardTooles, jsonData.CardToole{
				CardId:   cardTool.CardId,
				StarTime: cardTool.StarTime,
				JobType:  cardTool.JobType,
			})
		}
		// 将转换后的对象添加到 TaskInfoList
		translateInfoListMsg = append(translateInfoListMsg, taskInfo)
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0,
		TranslatesList: translateInfoListMsg,
		RequestId:      m.RequestId,
	})
}
func TranslateList(args []interface{}) {
	translate.TranslateList(args)
}
func UpdateTranslateLevel(args []interface{}) {
	m := args[0].(*jsonData.MsgUpdateTranslateLevelReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	ret, err := translate.UpdateTranslateLevel(m.UserId, m.TId, m.Step)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{
		Ret:       0,
		Result:    ret,
		RequestId: m.RequestId,
	})
}
func UpdateTranslateExpandLevel(args []interface{}) {
	m := args[0].(*jsonData.MsgExpandTranslateReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	ret, err := translate.UpdateTranslateExpandLevel(m.UserId, m.TId, m.Step)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: ret, RequestId: m.RequestId})
}
func DoIt(args []interface{}) {
	//translate.DoIt(args)
}
func GetBreakfast(args []interface{}) {
	m := args[0].(*jsonData.MsgGetBreakfastReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	userInfo, err := translate.GetBreakfastList(m.UserId)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 0, Message: err.Error(), Result: []string{}, RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: userInfo, RequestId: m.RequestId})
}
func UnlockBreakfastTools(args []interface{}) {
	m := args[0].(*jsonData.MsgUnlockBreakfastToolsReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	userInfo, err := translate.GetBreakfast(m.UserId, m.Cid)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	userRetInfo, err := userInfo.UnlockBreakfastTools(m.UserId, m.Cid, m.Tid)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: userRetInfo, RequestId: m.RequestId})
}
func UnlockBreakfastBreakfast(args []interface{}) {
	m := args[0].(*jsonData.MsgUnlockBreakfastBreakfastReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	userInfo, err := translate.GetBreakfast(m.UserId, m.Cid)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	userRetInfo, err := userInfo.UnlockBreakfastBreakfast(m.UserId, m.Cid, m.Bid)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: userRetInfo, RequestId: m.RequestId})
}
func UnlocksCareer(args []interface{}) {
	m := args[0].(*jsonData.MsgUnlocksCareerReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	userRetInfo, err := translate.UnlocksCareer(m.UserId, m.TTid, m.Cid)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: userRetInfo, RequestId: m.RequestId})
}
func UnlockCardTool(args []interface{}) {
	m := args[0].(*jsonData.MsgUnlockCardToolReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	ret, err := translate.UnlockCardTool(m.UserId, m.Tid, m.CardIndex)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: ret, RequestId: m.RequestId})
}
func PushCardTool(args []interface{}) {
	m := args[0].(*jsonData.MsgPushCardToolReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	ret, err := translate.PushCardTool(m.UserId, m.CardId, m.Tid, m.CardIndex, m.JobType)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: ret, RequestId: m.RequestId})
}
func ProduceBreakfast(args []interface{}) {
	m := args[0].(*jsonData.MsgProduceBreakfastReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	userInfo, err := translate.GetBreakfast(m.UserId, m.Cid)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	err = userInfo.ProduceBreakfast(m.BreakfastIndex, m.Num)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	Ret, err := translate.GetBreakfast(m.UserId, m.Cid)
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: Ret, RequestId: m.RequestId})
}
func FinishBreakfast(args []interface{}) {
	m := args[0].(*jsonData.MsgFinishBreakfastReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	userInfo, err := translate.GetBreakfast(m.UserId, m.Cid)
	item, num, err := userInfo.FinishBreakfast(m.BreakfastIndex, m.Num)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	var Ret = make(map[string]interface{}, 0)
	PlayerBreakfast, err := translate.GetBreakfast(m.UserId, m.Cid)
	Ret["player_breakfast"] = PlayerBreakfast
	Ret["item"] = item
	Ret["num"] = num
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: Ret, RequestId: m.RequestId})
}
func DaSao(args []interface{}) {
	m := args[0].(*jsonData.MsgDaoSaoReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	ret, err := translate.DaSao(m.UserId, m.TTid)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: ret, RequestId: m.RequestId})
}
func KaiYe(args []interface{}) {
	m := args[0].(*jsonData.MsgKaiYeReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	ret, err := translate.KaiYe(m.UserId, m.Tid)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: ret, RequestId: m.RequestId})

}
func ChaoYeShouYi(args []interface{}) {
	m := args[0].(*jsonData.MsgChanYeShouYiReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	ret, err := translate.GetTranslateShouYi(m.UserId)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: ret, RequestId: m.RequestId})

}
func TranslateNpcReward(args []interface{}) {
	m := args[0].(*jsonData.MsgTranslateNpcRewardReq)
	// 消息的发送者
	a := args[1].(gate.Agent)
	ret, err := translate.Reward(m.UserId, m.Tid, m.NpcId)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
		return
	}
	a.WriteMsg(&jsonData.MsgRes{Ret: 0, Result: ret, RequestId: m.RequestId})

}
